<?php


namespace App\Modules\System\Repositories;


use App\Models\VisitHistory;
use Flower\Core\Repository\Eloquent\BaseRepository;

class VisitorHistoryRepository extends BaseRepository
{

    public function model()
    {
        return VisitHistory::class;
    }


    public function add($storeId, $memberId, $source, $category, $linkName)
    {
        $data = [
            'store_id'  => $storeId,
            'member_id' => $memberId,
            'source'    => $source,
            'category'  => $category,
        ];
	    $res =  $this->model->where($data)->whereDate('visit_time',date("Y-m-d"))->first();
        if(isset($res)){
        	$res->visit_time= time();
        	$res->link_name = $linkName;
        	$res->count+=1;
        	$res->save();
        }else{
        	$data['visit_time'] = time();
        	$data['link_name'] = $linkName;
        	$data['count'] = 1;
        	$res = $this->create($data);
        }
        return $res;

    }

    /**
     * 写入订单访客表转化率
     * @param $storeId
     * @param $memberId
     * @param $date
     * @return mixed
     */
    public function updateVisitor($storeId,$memberId,$date){
        $info = $this
            ->model
            ->whereRaw('store_id ="'.$storeId.'" and member_id="'.$memberId.'" and  DATE_FORMAT(visit_time,\'%Y-%m-%d\') ="' . $date . '"')
            ->first();
        if(isset($info)){
            $info->is_order = 1;
            $info->save();
        }
        return $info;
    }
}
